package org.pictual.business.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.pictual.business.dao.exceptions.DAOException;
import org.pictual.business.vo.ImagenVO;


public class MySqlImagenDAO implements ImagenDAO {

	private Connection connection;
	private ResourceBundle queries=ResourceBundle.getBundle("org.pictual.business.dao.MySqlImagenDAO");
	protected static Logger log= Logger.getLogger(MySqlImagenDAO.class);
	
	public MySqlImagenDAO(){}
	
	public MySqlImagenDAO(Connection connection){
		this.connection=connection;
	}

	public ImagenVO readImagen(int imagen_id) throws DAOException {
		AccesoBD accesoBD = new AccesoBD();
		ResultSet rs=null;
		ImagenVO imagenVO = null;
		final String query=queries.getString("readImagen");
		
		try {
			rs = accesoBD.ejecutarConsulta(
					query, 
					imagen_id
					);
			
			if(rs!=null){
			
				while(rs.next()){
					imagenVO=new ImagenVO();
					
					imagenVO.setImagen_id(rs.getInt("imagen_id"));
					imagenVO.setWidth(rs.getInt("width"));
					imagenVO.setHeight(rs.getInt("height"));
					imagenVO.setPath_small(rs.getString("path_small"));
					imagenVO.setPath_original(rs.getString("path_big"));
					imagenVO.setRecomended_image(rs.getBoolean("recomended_image"));
					imagenVO.setRevised_image(rs.getBoolean("revised_image"));
					imagenVO.setTop_quality_revised(rs.getBoolean("top_quality_revised"));
					imagenVO.setExtraction_date(rs.getDate("extraction_date"));
					
					log.info("Obtencion de ImagenVO "+imagenVO.getImagen_id()+" de base de datos con exito");
			
				}
			}
		} catch (SQLException e) {
			log.error("Error al ejecutar: "+query, e);
			throw new DAOException(query, e);
		} finally{
			accesoBD.desconectar();
			accesoBD.cerrarResultSet(rs);
		}
		return imagenVO;
	}
	
	/**
	 * @deprecated
	 * @param imagen_id
	 * @return
	 * @throws DAOException
	 */
	public ImagenVO readImagen2(int imagen_id) throws DAOException {
		ResultSet rs=null;
		PreparedStatement ps=null;
		ImagenVO imagenVO = null;
		final String query=queries.getString("readImagen");
		try {
			if(null == ps){ 
				ps= connection.prepareStatement(query);
				ps.setInt(1, imagen_id);
				rs = ps.executeQuery();
				while(rs.next()){
					imagenVO=new ImagenVO();
					
					imagenVO.setImagen_id(rs.getInt("imagen_id"));
					imagenVO.setWidth(rs.getInt("width"));
					imagenVO.setHeight(rs.getInt("height"));
					imagenVO.setPath_small(rs.getString("path_small"));
					imagenVO.setPath_original(rs.getString("path_big"));
					imagenVO.setRecomended_image(rs.getBoolean("recomended_image"));
					imagenVO.setRevised_image(rs.getBoolean("revised_image"));
					imagenVO.setTop_quality_revised(rs.getBoolean("top_quality_revised"));
					imagenVO.setExtraction_date(rs.getDate("extraction_date"));
					
					log.info("Obtencion de ImagenVO "+imagenVO.getImagen_id()+" de base de datos con exito");
				}
			}

		} catch (SQLException e) {
			log.error("Error al ejecutar: "+query, e);
			throw new DAOException(query, e);
		} finally {
			try {
				rs.close();
				ps.close();
				connection.close();
			} catch (SQLException e) {
				log.error("Error al cerrar la consulta: "+query, e);
			}
		}
		
		return imagenVO;
	}

	/**
	 * AccesoBD
	 */
	public void insertImagen(ImagenVO imagenVO) throws DAOException {
		AccesoBD accesoBD = new AccesoBD();
		
		String query =queries.getString("insertImagen");
		
		try {
			accesoBD.ejecutarActualizacion(
					query,
					imagenVO.getImagen_id(),
					imagenVO.getWidth(),
					imagenVO.getHeight(),
					imagenVO.getPath_small(),
					imagenVO.getPath_original(),
					imagenVO.isRecomended_image(),
					imagenVO.isRevised_image(),
					imagenVO.isTop_quality_revised(),
					imagenVO.getExtraction_date(),
					imagenVO.getRevision_date()
					);
		} catch (SQLException e) {
			log.error("Error al ejecutar: "+query, e);
			throw new DAOException(query, e);
		} finally{
			accesoBD.desconectar();
		}
	}
	/**
	 * @deprecated
	 */
	public void insertImagen2(ImagenVO imagenVO) throws DAOException {
		String query =queries.getString("insertImagen");
		PreparedStatement ps=null;
		try {
			if(null == ps){
				ps=connection.prepareStatement(query);
				
				ps.setInt(1, imagenVO.getImagen_id());
				ps.setInt(2, imagenVO.getWidth());
				ps.setInt(3, imagenVO.getHeight());
				ps.setString(4, imagenVO.getPath_small());
				ps.setString(5, imagenVO.getPath_original());
				ps.setBoolean(6, imagenVO.isRecomended_image());
				ps.setBoolean(7, imagenVO.isRevised_image());
				ps.setBoolean(8, imagenVO.isTop_quality_revised());
				ps.setDate(9, (Date) imagenVO.getExtraction_date());
				ps.setDate(10, (Date) imagenVO.getRevision_date());
				
				ps.executeUpdate();
				log.info("Inserción de ImagenVO de"+imagenVO.getImagen_id()+" en base de datos con exito");
			}
		} catch (SQLException e) {
			log.error("Error al ejecutar: "+query, e);
			throw new DAOException(query, e);
		}  finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				log.error("Error al cerrar la consulta: "+query, e);
			}
		}
		
	}

	

}
